/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderableImage;
import java.util.Collection;
import javax.media.jai.CollectionImage;
import javax.media.jai.JAI;
import ca.forklabs.media.jai.operator.PipelineDescriptor;

class PipelineUtil {

//---------------------------
// Class variables
//---------------------------

   /**
    * Specializes the call to the correct JAI factory method.
    * @param   <T>   the type of image the specialization is for.
    */
   public static abstract class Specialization<T> {

      /**
       * Extracts the source object from the parameter block.
       * @param   pb   the parameter block.
       * @return   the source object.
       */
      public abstract T getSource(ParameterBlock pb);

      /**
       * Calls the correct JAI factory method.
       * @param   operator   the operator.
       * @param   pb   the parameter block.
       * @param   hints   rendering hints.
       * @return   the image created.
       */
      public abstract T create(String operator, ParameterBlock pb, RenderingHints hints);

      }

   /** Specialization for the rendered pipeline. */
   public static final Specialization<RenderedImage> RENDERED = new Specialization<RenderedImage>() {
      @Override
      public RenderedImage getSource(ParameterBlock pb) {
         RenderedImage source = null;

         CollectionImage collection = (CollectionImage) pb.getSource(0);
         if (0 != collection.size()) {
            source = (RenderedImage) collection.get(0);
            }

         return source;
         }
      @Override
      public RenderedImage create(String operator, ParameterBlock pb, RenderingHints hints) {
         RenderedImage image = JAI.create(operator, pb, hints);
         return image;
         }
      };

   /** Specialization for the renderable pipeline. */
   public static final Specialization<RenderableImage> RENDERABLE = new Specialization<RenderableImage>() {
      @Override
      public RenderableImage getSource(ParameterBlock pb) {
         RenderableImage source = null;

         CollectionImage collection = (CollectionImage) pb.getSource(0);
         if (0 != collection.size()) {
            source = (RenderableImage) collection.get(0);
            }

         return source;
         }
      @Override
      public RenderableImage create(String operator, ParameterBlock pb, RenderingHints hints) {
         RenderableImage image = JAI.createRenderable(operator, pb, hints);
         return image;
         }
      };

   /** Specialization for the collection pipeline. */
   public static final Specialization<Collection<?>> COLLECTION = new Specialization<Collection<?>>() {
      @Override
      public Collection<?> getSource(ParameterBlock pb) {
         Collection<?> source = (Collection<?>) pb.getSource(0);
         return source;
         }
      @Override
      public Collection<?> create(String operator, ParameterBlock pb, RenderingHints hints) {
         Collection<?> image = JAI.createCollection(operator, pb, hints);
         return image;
         }
      };

   /** Specialization for the renderable collection pipeline. */
   public static final Specialization<Collection<?>> RENDERABLE_COLLECTION = new Specialization<Collection<?>>() {
      @Override
      public Collection<?> getSource(ParameterBlock pb) {
         Collection<?> source = (Collection<?>) pb.getSource(0);
         return source;
         }
      @Override
      public Collection<?> create(String operator, ParameterBlock pb, RenderingHints hints) {
         Collection<?> image = JAI.createRenderableCollection(operator, pb, hints);
         return image;
         }
      };


//---------------------------
// Constructor
//---------------------------

   /**
    * Let no one instanciate.
    */
   private PipelineUtil() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Gets the name of the operations.
    * @param   pb   the parameter block.
    * @return   the names.
    */
   public static String[] getOperators(ParameterBlock pb) {
      String[] operators = (String[]) pb.getObjectParameter(PipelineDescriptor.OPERATIONS_PARAMETER_INDEX);
      return operators;
      }

   /**
    * Gets the parameter of the operations.
    * @param   pb   the parameter block.
    * @return   the parameters.
    */
   public static ParameterBlock[] getParameters(ParameterBlock pb) {
      ParameterBlock[] parameters = (ParameterBlock[]) pb.getObjectParameter(PipelineDescriptor.PARAMETERS_PARAMETER_INDEX);
      return parameters;
      }

   /**
    * Does the pipeline.
    * @param   pb   the parameter block.
    * @param   hints   rendering hints.
    * @param   specialization   specialization depending on the mode.
    *
    * @param   <T>   the type of image.
    *
    * @return   the result of the pipeline.
    */
   public static <T> T doPipeline(ParameterBlock pb, RenderingHints hints, Specialization<T> specialization) {
      T source = specialization.getSource(pb);
      String[] operators = PipelineUtil.getOperators(pb);
      ParameterBlock[] parameters = PipelineUtil.getParameters(pb);

   // for each operation, the source is
   // the sink of the last operation
      T sink = source;
      for (int i = 0, len = operators.length; i < len; i++) {
         String operator = operators[i];
         ParameterBlock parameter = parameters[i];

         if (null != sink) {
            parameter.addSource(sink);
            }

         sink = specialization.create(operator, parameter, hints);
         }

      return sink;
      }

   }
